import cv2
import numpy as np
from imghandler import *
from settings import *

def GetSize(img1,img2):
    r1,c1,d1=img1.shape
    r2,c2,d2=img2.shape
    r=min(r1,r2)
    c=min(c1,c2)
    return (r,c)

def CmpImg(img1,img2):
    r1,c1,d1=img1.shape
    r2,c2,d2=img2.shape
    if r1>r2 and c1>c2:
        return 1
    if r1<r2 and c1<c2:
        return 2
    return 0

def NormalizeSize(img1, img2):
    rows, cols, dpt = img2.shape
    dst = img1[:rows,:cols]
    return dst

def ReshapeImg(img,r,c):
    dst = img[:r,:c]
    return dst

def MergeImages(p1,p2,w1,w2):
    img1 = cv2.imread(p1)
    img2 = cv2.imread(p2)
    r=CmpImg(img1,img2)
    if r==1:
        img_mix = cv2.addWeighted(NormalizeSize(img1,img2), w1, img2, w2, 0)
    elif r==2:
        img_mix = cv2.addWeighted(img1, w1, NormalizeSize(img2,img1), w2, 0)
    else:
        rr,cc=GetSize(img1,img2)
        img_mix = cv2.addWeighted(ReshapeImg(img1,rr,cc), w1, ReshapeImg(img2,rr,cc), w2, 0)
    imgname=str(get_time_stamp())+'.jpg'
    ImgAddress=str(MEDIA_ROOT).replace('\\','/')+'/merge/'+imgname
    cv2.imwrite(ImgAddress,img_mix)